package com.xinjie.www.service.collect;

import com.xinjie.www.dao.BaseDao;
import com.xinjie.www.dao.collect.CollectDao;
import com.xinjie.www.dao.collect.CollectDaoImpl;
import com.xinjie.www.po.Collect;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CollectServiceImpl implements CollectService {
    private final CollectDao collectDao;

    public CollectServiceImpl() {
        collectDao = new CollectDaoImpl();
    }

    @Override
    public boolean doCollect(int postId, int userId, String time) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        boolean flag = false;
        conn = BaseDao.getConnection();
        if (conn != null) {
            int i = collectDao.doCollect(conn, postId, userId, time);
            if (i > 0) {
                flag = true;
            }
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    @Override
    public List<Collect> getCollect(int userId) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        conn = BaseDao.getConnection();
        List<Collect> collects = new ArrayList<>();
        if (conn != null) {
            collects = collectDao.getCollect(conn, userId);
            BaseDao.closeResource(conn, null, null);
        }
        return collects;
    }

    @Override
    public boolean cancelCollect(int collectId) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        conn = BaseDao.getConnection();
        boolean flag = false;
        if (conn != null) {
            int i = collectDao.cancelCollect(conn, collectId);
            if (i > 0) {
                flag = true;
            }
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }
}
